//
//  ToBeSurePeiTongCell.m
//  MyProject
//
//  Created by Pei on 16/8/5.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "ToBeSurePeiTongCell.h"
#import "UIView+Additions.h"

@implementation ToBeSurePeiTongCell
{
    UILabel *_headerLbl;
    UILabel *_dateLbl;
    
    UILabel *_titleLbl;
    UILabel *_detailLbl;
    
    UILabel *_statusLbl;
    
    UIImageView *_arrowImageView;
    UIView *_centerLineView;
}

@synthesize mSelected = _mSelected;
@synthesize passBtn;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _mSelected = NO;
        CGRect indicatorFrame = CGRectMake(-20, fabs(self.frame.size.height - 20)/ 2, 20, 20);
        _mSelectedIndicator = [[UIImageView alloc] initWithFrame:indicatorFrame];
        _mSelectedIndicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
        [self.contentView addSubview:_mSelectedIndicator];
        
        self.backgroundColor = kWhiteColor;
        
        _headerLbl = [[UILabel alloc] init];
        _headerLbl.frame = CGRectMake(10.0f, (80.0f-55.0f)/2.0f, 55.0f, 55.0f);
        _headerLbl.layer.masksToBounds = YES;
        _headerLbl.layer.cornerRadius = 55.0f/2.0f;
        _headerLbl.backgroundColor = [UIColor grayColor];
        _headerLbl.text = @"用电";
        _headerLbl.textColor = kWhiteColor;
        _headerLbl.textAlignment = NSTextAlignmentCenter;
        _headerLbl.font = kAppFont(Home_Big_Size+4.0f);
        [self addSubview:_headerLbl];
        
        _dateLbl = [[UILabel alloc] init];
        _dateLbl.frame = CGRectMake(kDeviceWidth-10.0f-70.0f, _headerLbl.origin.y, 0.0f, Home_Small_Size);
        _dateLbl.text = @"2016-06-23";
        _dateLbl.textAlignment = NSTextAlignmentRight;
        _dateLbl.font = kAppFont(Home_Small_Size);
        _dateLbl.textColor = kRGB(205, 206, 207);
        //[self addSubview:_dateLbl];
        
        _titleLbl = [[UILabel alloc] init];
        _titleLbl.frame = CGRectMake(_headerLbl.right+10.0f, _headerLbl.origin.y, kDeviceWidth-10.0f-10.0f*2.0f-_headerLbl.width, Home_Small_Size+2.0f);
        _titleLbl.text = @"检查任务-专项检查0001";
        _titleLbl.font = kAppFont(Home_Small_Size+2.0f);
        _titleLbl.textColor = kBlackColor;//kRGB(167, 168, 168);
        [self addSubview:_titleLbl];
        
        _detailLbl = [[UILabel alloc] init];
        _detailLbl.frame = CGRectMake(_titleLbl.origin.x, _titleLbl.bottom+5.0f, kDeviceWidth-10.0f-10.0f*2.0f-_headerLbl.width, Home_Small_Size);
        _detailLbl.text = @"公司-工区   机械";
        _detailLbl.font = kAppFont(Home_Small_Size);
        _detailLbl.textColor = kBlackColor;//kRGB(167, 168, 168);
        [self addSubview:_detailLbl];
        
        _arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"06jiantou-"]];
        _arrowImageView.frame = CGRectMake(kDeviceWidth-10.0f/2.0f-10.0f,_detailLbl.origin.y+(Home_Small_Size-(19.0f/2.0f))/2.0f, 10.0f/2.0f, 19.0f/2.0f);
        [self addSubview:_arrowImageView];
        
        _centerLineView = [[UIView alloc] initWithFrame:CGRectMake(_titleLbl.origin.x, _detailLbl.bottom+5.0f, kDeviceWidth-10.0f-10.0f*2.0f-_headerLbl.width, 0.5f)];
        _centerLineView.backgroundColor = kGreenColor;
        //[self addSubview:_centerLineView];
        
        passBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        passBtn.frame = CGRectMake(kDeviceWidth-10.0f-60.0f, _detailLbl.bottom+5.0f, 60.0f, 26.0f);
        [passBtn setBackgroundColor:kBtnBackgroundColor];
        [passBtn setTitle:@"确认" forState:UIControlStateNormal];
        [self addSubview:passBtn];
        
        _statusLbl = [[UILabel alloc] init];
        _statusLbl.frame = CGRectMake(_titleLbl.origin.x, _detailLbl.bottom+5.0f+8.0f, 200.0f, Home_Small_Size);
        _statusLbl.text = @"专项检查";
        _statusLbl.font = kAppFont(Home_Small_Size);
        _statusLbl.textColor = kRGB(95, 199, 247);
        [self addSubview:_statusLbl];
        
        
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 80.0f-0.5f, kDeviceWidth-10*2, 0.5f)];
        lineView.backgroundColor = kGreenColor;
        [self addSubview:lineView];
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    
    if (_mSelected)
    {
        if (((UITableView *)self.superview).isEditing)
        {
            self.backgroundView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:230.0/255.0 blue:250.0/255.0 alpha:1.0];
        }
        else
        {
            self.backgroundView.backgroundColor = [UIColor clearColor];
        }
        
        self.textLabel.textColor = [UIColor darkTextColor];
        [_mSelectedIndicator setImage:[UIImage imageNamed:@"checked_on"]];
        //self.accessoryType = UITableViewCellAccessoryCheckmark;
        
    }
    else
    {
        self.backgroundView.backgroundColor = [UIColor clearColor];
        self.textLabel.textColor = [UIColor grayColor];
        [_mSelectedIndicator setImage:[UIImage imageNamed:@"checked_off"]];
        self.accessoryType = UITableViewCellAccessoryNone;
        
    }
    
    
    
    [UIView commitAnimations];
    
    if (((UITableView *)self.superview.superview).isEditing)
    {
        _headerLbl.frame = CGRectMake(20+10+10.0f, (80.0f-55.0f)/2.0f, 0.0f, 55.0f);
        //_dateLbl.frame = CGRectMake(kDeviceWidth-10.0f-70.0f, _headerLbl.origin.y, 70.0f, Home_Small_Size);
        _titleLbl.frame = CGRectMake(_headerLbl.right+10.0f, _headerLbl.origin.y, kDeviceWidth-10.0f-10.0f*2.0f-_headerLbl.width-_dateLbl.width, Home_Small_Size+2.0f);
        _detailLbl.frame = CGRectMake(_titleLbl.origin.x, _titleLbl.bottom+5.0f, kDeviceWidth-10.0f-10.0f*2.0f-_headerLbl.width, Home_Small_Size);
        _arrowImageView.frame = CGRectMake(kDeviceWidth-10.0f/2.0f-10.0f,_detailLbl.origin.y+(Home_Small_Size-(19.0f/2.0f))/2.0f, 10.0f/2.0f, 19.0f/2.0f);
        //_centerLineView.frame = CGRectMake(_titleLbl.origin.x, _detailLbl.bottom+5.0f, kDeviceWidth-10.0f-10.0f*2.0f-_headerLbl.width, 0.5f);
        _statusLbl.frame = CGRectMake(_titleLbl.origin.x, _detailLbl.bottom+5.0f+8.0f, 200.0f, Home_Small_Size);
    }
    else
    {
        _headerLbl.frame = CGRectMake(10.0f, (80.0f-55.0f)/2.0f, 55.0f, 55.0f);
        //_dateLbl.frame = CGRectMake(kDeviceWidth-10.0f-70.0f, _headerLbl.origin.y, 70.0f, Home_Small_Size);
        _titleLbl.frame = CGRectMake(_headerLbl.right+10.0f, _headerLbl.origin.y, kDeviceWidth-10.0f-10.0f*2.0f-_headerLbl.width-_dateLbl.width, Home_Small_Size+2.0f);
        _detailLbl.frame = CGRectMake(_titleLbl.origin.x, _titleLbl.bottom+5.0f, kDeviceWidth-10.0f-10.0f*2.0f-_headerLbl.width, Home_Small_Size);
        _arrowImageView.frame = CGRectMake(kDeviceWidth-10.0f/2.0f-10.0f,_detailLbl.origin.y+(Home_Small_Size-(19.0f/2.0f))/2.0f, 10.0f/2.0f, 19.0f/2.0f);
        //_centerLineView.frame = CGRectMake(_titleLbl.origin.x, _detailLbl.bottom+5.0f, kDeviceWidth-10.0f-10.0f*2.0f-_headerLbl.width, 0.5f);
        _statusLbl.frame = CGRectMake(_titleLbl.origin.x, _detailLbl.bottom+5.0f+8.0f, 200.0f, Home_Small_Size);
    }
    
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    //[self setNeedsLayout];
}


- (void)showWithData:(NSDictionary*)data
{
    _titleLbl.text = [data valueForKey:@"InspectionNo"];
    //_dateLbl.text = [data valueForKey:@"InspectDate"];
    //NSDictionary *dic = [data objectForKey:@"InspectOrgAndProject"];
    _detailLbl.text = [NSString stringWithFormat:@"%@--%@", [data valueForKey:@"OrgName"],[data valueForKey:@"ProjectName"]];
    
    //NSDictionary *tempD = [data valueForKey:@"InspectType"];
    //_statusLbl.text = [tempD objectForKey:@"Name"];//[NSString stringWithFormat:@"%d", [[data valueForKey:@"InspectType"] intValue]];
    
    NSArray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:Check_Type_Items];
    for (NSDictionary *aaa in tempArray) {
        if ([[aaa valueForKey:@"Value"] intValue] == [[data valueForKey:@"InspectType"] intValue])
        {
            _statusLbl.text = [aaa objectForKey:@"Name"];
            
            NSString *str = [aaa objectForKey:@"Name"];
            str = [str substringWithRange:NSMakeRange(0,2)];
            
            //zh:综合  qt:其它  zx:专项  rc:日常
            //zh:综合  qt:其它  zx:专项  rc:日常
            if ([str isEqualToString:@"综合"]) {
                _headerLbl.backgroundColor = kRGB(111, 127, 206);
            }else if ([str isEqualToString:@"专项"]){
                _headerLbl.backgroundColor = kRGB(18, 113, 254);
            }else if ([str isEqualToString:@"日常"]){
                _headerLbl.backgroundColor = kRGB(113, 125, 159);
            }else if ([str isEqualToString:@"节假"]){
                _headerLbl.backgroundColor = kRGB(132, 143, 175);
            }else{
                _headerLbl.backgroundColor = kRGB(68, 203, 144);
            }
            
            _headerLbl.text = str;
        }
    }
}

- (void)showWithDataToBeSure:(NSDictionary*)data
{
    _titleLbl.text = [data valueForKey:@"InspectionName"];
    //_dateLbl.text = [data valueForKey:@"InspectDate"];
    //NSDictionary *dic = [data objectForKey:@"InspectOrgAndProject"];
    _detailLbl.text = [NSString stringWithFormat:@"%@-%@  %@", [data valueForKey:@"InspectOrgName"],[data valueForKey:@"InspectProjectName"],[data valueForKey:@"InspectorName"]];
    
    NSArray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:Check_Type_Items];
    for (NSDictionary *aaa in tempArray) {
        if ([[aaa valueForKey:@"Value"] intValue] == [[data valueForKey:@"InspectType"] intValue])
        {
            _statusLbl.text = [aaa objectForKey:@"Name"];
            
            NSString *str = [aaa objectForKey:@"Name"];
            str = [str substringWithRange:NSMakeRange(0,2)];
            
            //zh:综合  qt:其它  zx:专项  rc:日常
            //zh:综合  qt:其它  zx:专项  rc:日常
            if ([str isEqualToString:@"综合"]) {
                _headerLbl.backgroundColor = kRGB(111, 127, 206);
            }else if ([str isEqualToString:@"专项"]){
                _headerLbl.backgroundColor = kRGB(18, 113, 254);
            }else if ([str isEqualToString:@"日常"]){
                _headerLbl.backgroundColor = kRGB(113, 125, 159);
            }else if ([str isEqualToString:@"节假"]){
                _headerLbl.backgroundColor = kRGB(132, 143, 175);
            }else{
                _headerLbl.backgroundColor = kRGB(68, 203, 144);
            }
            
            _headerLbl.text = str;
        }
    }
}

- (void)showWithDataToBeChange:(NSDictionary*)data
{
    _titleLbl.text = [data valueForKey:@"InspectTaskName"];
    _dateLbl.text = [data valueForKey:@"DeadLineDate"];
    _detailLbl.text = [data valueForKey:@"InspectOrgName"];
    NSArray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:Check_Type_Items];
    for (NSDictionary *aaa in tempArray) {
        if ([[aaa valueForKey:@"Value"] intValue] == [[data valueForKey:@"InspectType"] intValue])
        {
            
            _statusLbl.text = [aaa objectForKey:@"Name"];
            
            NSString *str = [aaa objectForKey:@"Name"];
            str = [str substringWithRange:NSMakeRange(0,2)];
            
            //zh:综合  qt:其它  zx:专项  rc:日常
            //zh:综合  qt:其它  zx:专项  rc:日常
            if ([str isEqualToString:@"综合"]) {
                _headerLbl.backgroundColor = kRGB(111, 127, 206);
            }else if ([str isEqualToString:@"专项"]){
                _headerLbl.backgroundColor = kRGB(18, 113, 254);
            }else if ([str isEqualToString:@"日常"]){
                _headerLbl.backgroundColor = kRGB(113, 125, 159);
            }else if ([str isEqualToString:@"节假"]){
                _headerLbl.backgroundColor = kRGB(132, 143, 175);
            }else{
                _headerLbl.backgroundColor = kRGB(68, 203, 144);
            }
            
            _headerLbl.text = str;
        }
    }
    //_statusLbl.text = [NSString stringWithFormat:@"%d", [[data valueForKey:@"InspectType"] intValue]];
}

- (void)showWithDataToBeReviewed:(NSDictionary*)data
{
    _titleLbl.text = [data valueForKey:@"InspectTaskName"];
    _dateLbl.text = [data valueForKey:@"DeadLineDate"];
    _detailLbl.text = [data valueForKey:@"InspectOrgName"];
    NSArray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:Check_Type_Items];
    for (NSDictionary *aaa in tempArray) {
        if ([[aaa valueForKey:@"Value"] intValue] == [[data valueForKey:@"InspectType"] intValue])
        {
            _statusLbl.text = [aaa objectForKey:@"Name"];
            
            NSString *str = [aaa objectForKey:@"Name"];
            str = [str substringWithRange:NSMakeRange(0,2)];
            
            //zh:综合  qt:其它  zx:专项  rc:日常
            //zh:综合  qt:其它  zx:专项  rc:日常
            if ([str isEqualToString:@"综合"]) {
                _headerLbl.backgroundColor = kRGB(111, 127, 206);
            }else if ([str isEqualToString:@"专项"]){
                _headerLbl.backgroundColor = kRGB(18, 113, 254);
            }else if ([str isEqualToString:@"日常"]){
                _headerLbl.backgroundColor = kRGB(113, 125, 159);
            }else if ([str isEqualToString:@"节假"]){
                _headerLbl.backgroundColor = kRGB(132, 143, 175);
            }else{
                _headerLbl.backgroundColor = kRGB(68, 203, 144);
            }
            _headerLbl.text = str;
        }
    }
    //_statusLbl.text = [NSString stringWithFormat:@"%d", [[data valueForKey:@"InspectType"] intValue]];
}


#pragma mark -
#pragma mark Public

- (void)changeMSelectedState
{
    _mSelected = !_mSelected;
    [self setNeedsLayout];
}

@end
